package com.dianmi.seckill.common.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @Description:
 * @Author: guozongping
 * @CreateDate: 2020/8/6 18:50
 */
@Slf4j
public class FastUtils {

    public static <T> T getNullElse(T t, T e) {
        return t == null ? e : t;
    }

    public static String getBlankElse(String t, String e) {
        return t == null || "".equals(t.trim()) ? e : t;
    }

    public static <T> T convertCopy(Class<T> tClass, Object source) {
        try {
            T t = tClass.newInstance();
            BeanUtils.copyProperties(source, t);
            return t;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> List<T> convertCopyList(Class<T> tClass, List<?> sourceList) {
        List<T> list = new LinkedList<>();
        if (sourceList == null) {
            return list;
        }
        for (Object o : sourceList) {
            T t = convertCopy(tClass, o);
            list.add(t);
        }
        return list;
    }

    public static <F, T> Map<F, T> toFieldMap(Function<T, F> function, List<T> list) {
        if (list == null) {
            return new HashMap<>();
        }
        Map<F, T> map = new HashMap<>();
        for (T t : list) {
            map.put(function.apply(t), t);
        }
        return map;
    }

    public static <F, T> List<F> toFieldList(Function<T, F> function, List<T> list) {
        if (list == null) {
            return new LinkedList<>();
        }
        List<F> res = new LinkedList<>();
        for (T t : list) {
            res.add(function.apply(t));
        }
        return res;
    }

    public static <F extends Comparable<F>, T> List<T> toSort(List<T> list, Function<T, F> function, boolean isAsc, boolean nullLast) {
        list.sort((o1, o2) -> {
            F f1 = function.apply(o1);
            F f2 = function.apply(o2);
            int i;
            if (f1 == null && f2 == null) {
                i = 0;
            } else if (f1 == null) {
                i = 1;
            } else if (f2 == null) {
                i = -1;
            } else {
                i = f1.compareTo(f2);
                //增序还是降序
                return isAsc ? i : -i;
            }
            //空值放最后还是最前
            return nullLast ? i : -i;
        });
        return list;
    }

    public static <T, K> T mapToObject(Map<String, K> map, T t) {
        if (null == map || null == t) {
            return null;
        }
        map.forEach((k, v) -> {
            try {
                Field declaredField = t.getClass().getDeclaredField(k);
                declaredField.setAccessible(true);
                declaredField.set(t, v);
            } catch (Exception e) {
                log.error("map转对象错误", e);
            }
        });
        return t;
    }



}
